1
|
|
|
/** |
2
|
|
|
* cookiejs object for setting/getting/removing cookies |
3
|
|
|
*/ |
4
|
|
|
(function(root, factory) { |
5
|
|
|
if (typeof define === "function" && define.amd) { |
|
|
|
|
6
|
|
|
define(["cookiejs"], factory); |
7
|
|
|
} else if (typeof module === "object" && module.exports) { |
8
|
|
|
module.exports = factory(); |
9
|
|
|
} else { |
10
|
|
|
root.cookiejs = factory(); |
11
|
|
|
} |
12
|
|
|
}(this, function() { |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @type {Object} |
16
|
|
|
*/ |
17
|
|
|
var cookiejs = {}; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* sets or overwrites a cookie |
21
|
|
|
* |
22
|
|
|
* @param {String} sCookieName - the name of the cookie you want to set |
23
|
|
|
* @param {String} sValue - the value you want to set |
24
|
|
|
* @param {String} oAttributes - options e.g. domain, path, expires |
25
|
|
|
* |
26
|
|
|
* @returns {String} |
27
|
|
|
*/ |
28
|
|
|
cookiejs.set = function(sCookieName, sValue, oAttributes) { |
29
|
|
|
var sAttributes = ''; |
30
|
|
|
|
31
|
|
|
oAttributes = oAttributes || {}; |
|
|
|
|
32
|
|
|
|
33
|
|
|
if (typeof oAttributes.path !== 'string') { |
34
|
|
|
sAttributes += '; path=/'; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
Object.keys(oAttributes).forEach(function(sAttributeName) { |
38
|
|
|
sAttributes += ';' + sAttributeName + '=' + oAttributes[sAttributeName]; |
39
|
|
|
}); |
40
|
|
|
|
41
|
|
|
document.cookie = encodeURIComponent(sCookieName) + '=' + encodeURIComponent(sValue) + sAttributes; |
42
|
|
|
}; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* returns the value of a cookie |
46
|
|
|
* |
47
|
|
|
* @param {String} sCookieName |
48
|
|
|
* |
49
|
|
|
* @returns {String|Boolean} |
50
|
|
|
*/ |
51
|
|
|
cookiejs.get = function(sCookieName) { |
52
|
|
|
var aCookies, |
53
|
|
|
gCookieValue = false; |
54
|
|
|
|
55
|
|
|
if (!sCookieName || typeof sCookieName != 'string') { |
56
|
|
|
return gCookieValue; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
aCookies = document.cookie.split('; '); |
60
|
|
|
|
61
|
|
|
for (var i = aCookies.length - 1; i >= 0; i--) { |
62
|
|
|
if (decodeURIComponent(aCookies[i].split('=')[0]) === sCookieName) { |
63
|
|
|
gCookieValue = decodeURIComponent(aCookie[1]); |
|
|
|
|
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return gCookieValue; |
68
|
|
|
}; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* removes a specific cookie |
72
|
|
|
* |
73
|
|
|
* oAttributes must contain the correct path and domain else you can't remove the cookie |
74
|
|
|
* |
75
|
|
|
* @param {String} sCookieName |
76
|
|
|
* @param {Object} oAttributes - options e.g. domain, path, expires |
77
|
|
|
*/ |
78
|
|
|
cookiejs.remove = function(sCookieName, oAttributes) { |
79
|
|
|
oAttributes = oAttributes || {}; |
|
|
|
|
80
|
|
|
oAttributes.expires = 'Thu, 01 Jan 1970 00:00:01 GMT'; |
81
|
|
|
this.set(sCookieName, '', oAttributes); |
82
|
|
|
}; |
83
|
|
|
|
84
|
|
|
return cookiejs; |
85
|
|
|
})); |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.